<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@page  import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Detail</title>
<link rel="stylesheet" href="css/stylev2.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery/jquery-2.1.1.min.js"></script>
<script src="js/FileSaver.js"></script>
<script src="js/jquery-student-detail-wordexport.js"></script>

<script type="text/javascript" src="js/jspdf.js"></script>
<script type="text/javascript" src="js/jspdf/jspdf.plugin.standard_fonts_metrics.js"></script> 
<script type="text/javascript" src="js/jspdf/jspdf.plugin.split_text_to_size.js"></script>               
<script type="text/javascript" src="js/jspdf/jspdf.plugin.from_html.js"></script>
<script type="text/javascript" src="js/jspdf/jspdf.plugin.addimage.js"></script>
<script type="text/javascript" src="js/jspdf/jspdf.debug.js"></script>

	<style type="text/css">
		a {
	      text-decoration:none;
	   }
	</style>



<script type="text/javascript">

jQuery(document).ready(function($) {
	
	$("#result-content").hide();
	
	
	

});


</script>
<style type="text/css">

.silvergrad:hover {
 
    cursor: pointer;
   
}


</style>

</head>
<body>

<%
	// get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
	

	// check session
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin_home.jsp"> 	
	<%}
	
//////////get permission ///////////
	
	String name = String.valueOf(session.getAttribute("name"));
	String surname = String.valueOf(session.getAttribute("surname"));
	String role_name = String.valueOf(session.getAttribute("student_role"));
	
	String department_id = String.valueOf(session.getAttribute("department_id"));

	System.out.println("name : " + name);///////////////////////////////
	System.out.println("department_id : " + department_id);/////////////////////////
	
	
	List<String> system_name = new ArrayList<String>();
	
	Permission permission = new Permission(strUser,name);
	permission.setDepartmentId(department_id);
	permission.setStudentCreate(Boolean.valueOf(String.valueOf(session.getAttribute("student_create"))));
	permission.setStudentDelete(Boolean.valueOf(String.valueOf(session.getAttribute("student_delete"))));
	permission.setStudentRead(Boolean.valueOf(String.valueOf(session.getAttribute("student_read"))));
	permission.setStudentUpdate(Boolean.valueOf(String.valueOf(session.getAttribute("student_update"))));
	
	String permiss = "";
	if(permission.getStudentCreate()) {
		permiss = permiss + "Add";
	}
	if(permission.getStudentUpdate()) {
		if("".equals(permiss)) {
			permiss = permiss + "Edit";
		} else {
			permiss = permiss + " Edit";
		}
	}
	if(permission.getStudentRead()) {
		if("".equals(permiss)) {
			permiss = permiss + "Read";
		} else {
			permiss = permiss + " Read";
		}
	}
	if(permission.getStudentDelete()) {
		if("".equals(permiss)) {
			permiss = permiss + "Delete";
		} else {
			permiss = permiss + " Delete";
		}
	}
	//check permission
	if(!permission.getStudentCreate()){%>
		<script>alert("This user dont have PERMISSION to access this zone.");</script>
		<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=student_home.jsp"> 	
	<%}
	
	//set Database Connection
	String hostProps = "";
	String usernameProps  = "";
	String passwordProps  = "";
	
	try {
		//get current path
		ServletContext servletContext = request.getSession().getServletContext();
		
		InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
		Properties props = new Properties();
		
		props.load(input);

		hostProps  = props.getProperty("host");
		usernameProps  = props.getProperty("username");
		passwordProps  = props.getProperty("password");
	} catch (Exception e) { 
		out.println(e);  
	}

	System.out.println("hostProperties : "+hostProps);////////////////try/////////////////
	System.out.println("usernameProperties : "+usernameProps);////////////////try/////////////////
	System.out.println("passwordProperties : "+passwordProps);////////////////try/////////////////
	
	// connect database
	Connection connect = null;		
	try {
		Class.forName("com.mysql.jdbc.Driver");
	
		connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/AIMS" +
				"?user=" + usernameProps  + "&password=" + passwordProps );
	
		if(connect != null){
			System.out.println("Database Connect Sucesses.");
		} else {
			System.out.println("Database Connect Failed.");	
		}

	} catch (Exception e) {
		out.println(e.getMessage());
		e.printStackTrace();
	}
	
	
	String student_id = request.getParameter("student_id");
	System.out.println("@ student_add_result student_id:"+student_id);
	String entrance_id="";
	String curr_id="";
	String lecturer_id="";
	
	Student stu = new Student();
	
	
	ResultSet rs = 
			connect.createStatement().executeQuery(" SELECT S.picture , P.personal_id , N.title_th ,"+
					" N.name_th, N.surname_th , N.name_en , N.surname_en , P.gender,P.marital_status ,"+
				    " P.address , P.phone_number, P.email , P.facebook , S.father_name , S.father_address ,"+
					" S.father_phone ,S.mother_name , S.mother_address , S.mother_phone , S.parent_name,"+
				    " S.parent_address ,S.parent_phone ,S.student_id , S.year, S.entrance_id  , S.school ,"+
					" S.gpa , ST.status_name ,D.department_name_th, S.curriculum_id ,P.date_of_birth ,S.lecturer_id "+
				    " FROM student S, full_name N, department D, status ST, people P "+
				    " WHERE S.personal_id = P.personal_id AND P.full_name_id = N.full_name_id "+
				    " AND S.department_id = D.department_id AND S.status_id = ST.status_id "+
				    " AND S.student_id='"+student_id+"'");
	
	
	while(rs.next())
	{


		stu.setPicture(rs.getString("picture"));
		stu.setPersonalId(rs.getString("personal_id"));
		stu.setTitle(rs.getString("title_th"));
		stu.setNameTH(rs.getString("name_th"));
		stu.setSurnameTH(rs.getString("surname_th"));
		stu.setNameEN(rs.getString("name_en"));
		stu.setSurnameEN(rs.getString("surname_en"));
		stu.setBirtgdate(rs.getString("date_of_birth"));
		if(rs.getString("gender").equals("M"))
		{
			stu.setGender("ชาย");
		}else
		{
			stu.setGender("หญิง");
		}
		
		stu.setMaritalStatus(rs.getString("marital_status"));
		stu.setAddress(rs.getString("address"));
		stu.setTelephone(rs.getString("phone_number"));
		stu.setEmail(rs.getString("email"));
		stu.setFacebook(rs.getString("facebook"));
		stu.setFatherName(rs.getString("father_name"));
		stu.setFatherAddress(rs.getString("father_address"));
		stu.setFatherTelephone(rs.getString("father_phone"));
		stu.setMotherName(rs.getString("mother_name"));
		stu.setMotherAddress(rs.getString("mother_address"));
		stu.setMotherTelephone(rs.getString("mother_phone"));
		stu.setParentName(rs.getString("parent_name"));
		stu.setParentAddress(rs.getString("parent_address"));
		stu.setParentTelephone(rs.getString("parent_phone"));
		stu.setStudentId(rs.getString("student_id"));
		stu.setYear(rs.getString("year"));
		stu.setSchool(rs.getString("school"));
		stu.setGpa(rs.getString("gpa"));
		stu.setStatus(rs.getString("status_name"));
		stu.setDepartment(rs.getString("department_name_th"));
		
	    entrance_id = rs.getString("entrance_id");
		curr_id = rs.getString("curriculum_id");
		lecturer_id = rs.getString("lecturer_id");
		
		
	}		
	ResultSet rs_ent = connect.createStatement().executeQuery(" SELECT entrance_name " +
															  " FROM entrance " +
															  " WHERE entrance_id='"+entrance_id+"'");
		while(rs_ent.next()){
			stu.setEntrance(rs_ent.getString("entrance_name"));
		}
		
	ResultSet rs_curr =  connect.createStatement().executeQuery(" SELECT curriculum_name_th , degree "+
																" FROM curriculum "+
																" WHERE curriculum_id='"+curr_id+"'");
		while(rs_curr.next()){
			stu.setCurriculumName(rs_curr.getString("curriculum_name_th"));
			stu.setDegree(rs_curr.getString("degree"));
		}
		
	ResultSet rs_lec = connect.createStatement().executeQuery(" SELECT *" +
															  " FROM lecturer L , full_name N "+
															  " WHERE L.personal_id = N.personal_id "+		
															  " AND L.lecturer_id='"+lecturer_id+"'");
		while(rs_lec.next()){
			stu.setLecturer(rs_lec.getString("name_th")+" "+rs_lec.getString("surname_th"));
		}
			
		connect.close();

%>


<div id="page-wrap">
		<div id="inside">
			<table border="1">
				<tr style="font-size:24px">
					<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
						<div id="header_banner-2"></div>
					</td>	
					
					<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
						<p align="right"><label><font size="4" color="#ffffff">username : <%=strUser%>&nbsp;&nbsp;</font></label></p>
						<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
					</td>
				</tr>
			</table>
			<table border="0">
				<tr style="font-size:10px">
				
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><h2><a href="student_home.jsp"><font color="#273b80"><label>หน้าแรก</label></font></a></h2></center>
					</td>
	<%
			if(permission.getStudentRead()) {
	%>
					<td width = "720" height="50" style= "background-color:#FFFFFF ; border-bottom-color: #FFFFFF;">
						<center><h2><font color="#273b80"><label>ค้นหา</label></font></h2></center>
					</td>
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="student_summary.jsp"><h2><font color="#273b80"><label>สรุปจำนวนนักศึกษา</label></font></h2></a></center>
					</td>
	<%
			}
			if(permission.getStudentCreate()) {
	%>
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="student_add.jsp"><h2><font color="#273b80"><label>เพิ่มนักศึกษา</label></font></h2></center>
					</td>
	<%
			}
			
			if(permission.getStudentUpdate()) {
	%>
					<td width = "720" height="50" style= "background-color:#D0D0D0 ;">
						<center><a href="student_edit.jsp"><h2><font color="#273b80"><label>แก้ไขข้อมูล</label></font></h2></a></center>
					</td>
	<%
			}
	%>
				</tr>
			</table>	

				
					
			
			<div style="clear: both;"></div>
				
			<div id="footer"></div>
			
		</div>		
			<div style="clear: both;"></div>
   </div>




</body>
</html>